 $authr()
 require('../css/projectTrain.less')

 require('../font/iconfont.css')

 document.ready(function () {





     const timeArr = []

     const progress = document.querySelector("#progress");
     const endRun = document.querySelector("#endRun");
     //  获取视频
     const videoQueue = JSON.parse(localStorage.getItem('currentData'))
     const watchVideo = document.querySelector("#watchVideo");
     let index = 0

     let timer;



     let count = document.querySelector('#currentCount')
     let allcount = document.querySelector('#allCount')
     let tits = document.querySelector('#currentTitle')

     //  播放
     function playVideo() {
         watchVideo.src = BASE_URL + videoQueue[index].videoUrl
         count.innerHTML = index + 1
         allcount.innerHTML = videoQueue.length
         tits.innerHTML = videoQueue[index].title
     }

     playVideo()


     let stopMask = document.querySelector('#stopMask');

     //  暂停
     document.querySelector("#stopping").addEventListener('click', function () {

         timeArr[index] = watchVideo.currentTime.toFixed(2)
         stopMask.classList.remove("none")
         watchVideo.pause()
         //  清除定时器
         clearInterval(timer)
         //  存储当前视频的时间
     })
     //  继续
     document.querySelector('#continueRun').addEventListener('click', function () {
         stopMask.classList.add("none")
         watchVideo.play()
         //  调用定时器
         showProgress()
     })
     //  上一个
     document.querySelector("#preVideo").addEventListener('click', function () {
         index--
         if (index < 0) {
             index = videoQueue.length - 1
         }
         playVideo()
     })
     //  下一个
     document.querySelector("#nextVideo").addEventListener('click', function () {

         timeArr[index] = watchVideo.currentTime.toFixed(2)
         index++
         if (index > videoQueue.length - 1) {
             index = 0
         }
         playVideo()

     })
     //  播放中
     watchVideo.addEventListener('play', function () {
         showProgress()
     })



     //  播放完成
     watchVideo.addEventListener('ended', function () {
         index++
         if (index == 4) {
             index = 0
         }
         playVideo()
         watchVideo.play()

     })


     //  结束播放->
     endRun.addEventListener('click', function () {
         const cid = sessionStorage.getItem('cid')
         const uid = JSON.parse(localStorage.getExpire('data')).user.userId
         
        const alltime = timeArr.reduce((add, cur) => {
             return Number(add)+Number(cur)
        })
        
        //  发送请求
         axios.post(`${BASE_URL}/sports/saveTrain`, {
             id: cid,
             userId: uid,
             takeTime: alltime,
             calorie: alltime * 0.5
         }).then(res => {
             if (res.data.status == 0) {
                 location.href = `./projectDes.html?id=${cid}`
             }
         })
         
     })

     //  显示播放进度
     function showProgress() {

         timer = setInterval(() => {
             let currentWidth = (watchVideo.currentTime / watchVideo.duration) * watchVideo.clientWidth
             progress.style.width = currentWidth + "px"

         }, 200)

     }


 })